Skip to content

fix(anthropic): Only finish relevant spans in .create() patches#5716

Open
alexander-alderman-webb wants to merge 5 commits intowebb/anthropic/separate-sync-and-asyncfrom
webb/anthropic/remove-get-current-span
Open

fix(anthropic): Only finish relevant spans in .create() patches#5716
alexander-alderman-webb wants to merge 5 commits intowebb/anthropic/separate-sync-and-asyncfrom
webb/anthropic/remove-get-current-span

Conversation

@alexander-alderman-webb
Copy link
Copy Markdown
Contributor

@alexander-alderman-webb alexander-alderman-webb commented Mar 20, 2026

Description

Anthropic spans are only created in _sentry_patched_create() and _sentry_patched_create_async().

Exit spans in these functions when the anthropic library function raises an exception.

Resolves an edge case where _sentry_patched_create() or _sentry_patched_create_async() exits early and does not create a span and get_current_span() therefore returns a non-anthropic span.

Issues

Reminders

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 20, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (logging) Separate ignore lists for events/breadcrumbs and sentry logs by sl0thentr0py in #5698

Bug Fixes 🐛

Anthropic

  • Only finish relevant spans in .create() patches by alexander-alderman-webb in #5716
  • Set exception info on streaming span when applicable by alexander-alderman-webb in #5683
  • Patch AsyncStream.close() and AsyncMessageStream.close() to finish spans by alexander-alderman-webb in #5675
  • Patch Stream.close() and MessageStream.close() to finish spans by alexander-alderman-webb in #5674

Documentation 📚

  • Add note on AI PRs to CONTRIBUTING.md by sentrivana in #5696

Internal Changes 🔧

  • (anthropic) Separate sync and async .create() patches by alexander-alderman-webb in #5715
  • Add -latest alias for each integration test suite by sentrivana in #5706
  • Use date-based branch names for toxgen PRs by sentrivana in #5704
  • 🤖 Update test matrix with new releases (03/19) by github-actions in #5703
  • Add client report tests for span streaming by sentrivana in #5677

Other

  • Update CHANGELOG.md by sentrivana in #5685

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 20, 2026

Codecov Results 📊

10 passed | Total: 10 | Pass Rate: 100% | Execution Time: 2.87s

All tests are passing successfully.

❌ Patch coverage is 20.00%. Project has 15227 uncovered lines.

Files with missing lines (1)
File Patch % Lines
sentry_sdk/integrations/anthropic.py 20.00% ⚠️ 4 Missing

Generated by Codecov Action

@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review April 2, 2026 08:55
@alexander-alderman-webb alexander-alderman-webb requested a review from a team as a code owner April 2, 2026 08:55
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Copy link
Copy Markdown
Member

@ericapisani ericapisani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any test coverage that we can add here to confirm this behaviour? Otherwise LGTM

@alexander-alderman-webb
Copy link
Copy Markdown
Contributor Author

Is there any test coverage that we can add here to confirm this behaviour? Otherwise LGTM

In my view a regression test has limited use in this case because an integration should not be touching spans created outside the integration (unless it's for an agreed-upon reason, i.e., not duplicating spans between openai-agents and openai).

For the future, here's a script that verifies that demonstrates the edge case I'm addressing below. The messages argument is missing from the call to create() and the outer transaction is closed prematurely since it's status is INTERNAL_ERROR.

import sentry_sdk
from sentry_sdk.tracing import SPANSTATUS
from sentry_sdk.integrations.anthropic import AnthropicIntegration
from anthropic import Anthropic

sentry_sdk.init(
    dsn="",
    integrations=[AnthropicIntegration()],
    traces_sample_rate=1.0,
)

def main():
    client = Anthropic(api_key="dummy")
    with sentry_sdk.start_transaction(name="parent", op="test") as transaction:
        transaction.set_status(SPANSTATUS.INTERNAL_ERROR)
        try:
            client.messages.create(max_tokens=1024, model="model", stream=False)
        except Exception as e:
            print(f"Expected exception: {e}")

if __name__ == "__main__":
    main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants